/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Delivery;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.DeliveryService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * 物流公司
 *
 * @author LGH
 * @date 2019-10-29 11:42:24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/delivery" )
public class DeliveryController {

    private final DeliveryService deliveryService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param delivery 物流公司
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<Delivery>> getDeliveryPage(PageParam<Delivery> page, Delivery delivery) {
        return ResponseEntity.ok(deliveryService.page(page, new LambdaQueryWrapper<Delivery>()
                .like(StrUtil.isNotBlank(delivery.getDvyName()),Delivery::getDvyName,delivery.getDvyName())
                .like(StrUtil.isNotBlank(delivery.getDvyNo()),Delivery::getDvyNo,delivery.getDvyNo())
                .like(StrUtil.isNotBlank(delivery.getDvyNoHd()),Delivery::getDvyNoHd,delivery.getDvyNoHd())
        ));
    }


    /**
     * 通过id查询物流公司
     * @param dvyId id
     * @return 单个数据
     */
    @GetMapping("/info/{dvyId}" )
    public ResponseEntity<Delivery> getById(@PathVariable("dvyId") Long dvyId) {
        return ResponseEntity.ok(deliveryService.getById(dvyId));
    }

    /**
     * 新增物流公司
     * @param delivery 物流公司
     * @return 是否新增成功
     */
    @SysLog("新增物流公司" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform:delivery:save')" )
    public ResponseEntity<Boolean> save(@RequestBody @Valid Delivery delivery) {
        delivery.setRecTime(new Date());
        delivery.setModifyTime(new Date());
        return ResponseEntity.ok(deliveryService.save(delivery));
    }

    /**
     * 修改物流公司
     * @param delivery 物流公司
     * @return 是否修改成功
     */
    @SysLog("修改物流公司" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform:delivery:update')" )
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid Delivery delivery) {
        delivery.setModifyTime(new Date());
        return ResponseEntity.ok(deliveryService.updateById(delivery));
    }


    /**
     * 通过id删除物流公司
     * @param dvyId id
     * @return 是否删除成功
     */
    @SysLog("删除物流公司" )
    @DeleteMapping("/{dvyId}" )
    @PreAuthorize("@pms.hasPermission('platform:delivery:delete')" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long dvyId) {
        return ResponseEntity.ok(deliveryService.removeById(dvyId));
    }

    /**
     * 分页获取
     */
    @GetMapping("/list")
    public ResponseEntity<List<Delivery>> page(){

        List<Delivery> list = deliveryService.list();
        return ResponseEntity.ok(list);
    }
}
